﻿<%
class BlogClass
    public property get CACHE_PREFIX 
        CACHE_PREFIX = "BLOG"
    end property
    
    public blogId, blogTitle, blogDesc, blogOwnerId, blogOwnerName
    public bannerUrl, photoUrl, pageSize, showContent
    
    private m_blogCatIDs, m_topItemIDs, m_topComments, m_linkGroups
    private m_neighbors

    private sub class_initialize()

    end sub
    
    private sub class_terminate()
    
    end sub
    
    public function build(array)
        build = false
        if isEmpty(array) or not isArray(array) then
            exit function
        end if
        if ubound(array)<13 then
            exit function
        end if
        
        blogId          = array(0)
        blogTitle       = array(1)
        blogDesc        = array(2) 
        blogOwnerId     = array(3) 
        blogOwnerName   = array(4) 
        bannerUrl       = array(5) 
        photoUrl        = array(6) 
        pageSize        = array(7) 
        showContent     = array(8) 
        m_blogCatIDs    = array(9)
        m_topItemIDs    = array(10)
        m_topComments   = array(11)
        m_linkGroups    = array(12)   

        
        build = true
    end function
    
    public function buildFromRS(rs)
        buildFromRS = false
        if isEmpty(rs) or not isObject(rs) then
            exit function
        end if
        if rs.bof or rs.eof then
            exit function
        end if
        
        blogId  = rs("blog_id")

        on error resume next

        blogTitle = rs("blog_Title")
        blogDesc = rs("blog_Desc")
        bannerUrl = rs("banner_Url")
        photoUrl = rs("photo_Url")
        pageSize = rs("page_Size")
        showContent = rs("show_Content")
        
        blogOwnerId = clng(rs("user_id"))
        blogOwnerName = rs("user_name")

        on error goto 0
                
        buildFromRS = true
    end function
    

    public function toArray()
        dim array(13)
        array(0) = blogId
        array(1) = blogTitle
        array(2) = blogDesc
        array(3) = blogOwnerId
        array(4) = blogOwnerName
        array(5) = bannerUrl
        array(6) = photoUrl
        array(7) = pageSize
        array(8) = showContent
        array(9) = m_blogCatIDs
        array(10) = m_topItemIDs 
        array(11) = m_topComments 
        array(12) = m_linkGroups
        
        toArray = array
    end function
    

    public function load(blog_Id)
        load = false
        if isEmpty(blog_Id) then
            exit function
        end if
        
        blogId= clng(blog_id)
        
        load = loadFromCache
        if load=false then
            load = loadFromDB()
            if (load=true) then
                call cache()
            end if    
        end if    
    end function
    
    public function reload(blog_Id)
        reload = false
        if isEmpty(blog_Id) then
            exit function
        end if
        
        blogId= clng(blog_id)

        reload = loadFromDB()
        if reload=true then
            call cache()
        else
            call clean()
        end if    
    end function
    
    public function clean()
        call removeFromCache(CACHE_PREFIX, blogId)
        blogId          = Empty
        blogTitle       = Empty
        blogDesc        = Empty
        blogOwnerId     = Empty
        blogOwnerName   = Empty
        bannerUrl       = Empty
        photoUrl        = Empty
        pageSize        = Empty
        showContent     = Empty
        m_blogCatIDs    = Empty
        m_topItemIDs    = Empty
        m_topComments   = Empty
        m_linkGroups    = Empty   
    end function
    
    
    private function loadFromCache()
        loadFromCache = build(getFromCache(CACHE_PREFIX, blogId))
    end function
        
    public sub cache()
       if not isEmpty(blogId) then
           call build(putIntoCache(CACHE_PREFIX, blogId, toArray()))
       end if
    end sub
    

    private function loadFromDB()
        'response.write "loadFromDB"

        call openDB
                
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")

        sql = " select blog_id, blog_title, blog_desc,banner_Url, photo_Url, page_Size, show_Content, user_id, user_name from blog where blog_id=" & blogId
        rs.open sql, conn, 1, 1
        if rs.eof then
            loadFromDB = false
            exit function
        end if

        call buildFromRS(rs)        
    
        
        'call loadBlogCatsFromDB()
        'call loadTopItemsFromDB()
        'call loadTopCommentsFromDB()
        rs.close
        set rs = nothing
        
        loadFromDB = true
    end function
    
    
    
    public sub loadBlogCatsFromDB()
        call openDB

        dim i
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")

        sql = " select cat_id from blog_category where blog_id=" & blogID & " order by display_seq"
        
        rs.open sql,conn,1,1

        dim count
        count = rs.recordCount
        redim m_blogCatIDs(count)
        if count>0 then
            for i=0 to count -1        
                m_blogCatIDs(i) = rs(0)
                rs.moveNext()
            next
        end if    

        rs.close
        set rs = nothing
    end sub

    public function getBlogCats()
        if isEmpty(m_blogCatIDs) then
            call loadBlogCatsFromDB()
            call cache()
        end if
        
        dim count, i,  blogCat
        count = ubound(m_blogCatIDs)
        if count<1 then
            exit function
        end if 
        
        dim blogCats
        redim blogCats(count+2)
        
        set blogCat = new BlogCatClass
        call blogCat.load(blogID, Empty)
        blogCat.catName = "首页"
        set blogCats(0) = blogCat
        for i=1 to count
            set blogCat = new BlogCatClass
            call blogCat.load(blogID, m_blogCatIDs(i-1))
            set blogCats(i) = blogCat
        next
        set blogCat = new BlogCatClass
        call blogCat.load(blogID, null)
        blogCat.catName = "未归类"
        set blogCats(count+1) = blogCat
        
        getBlogCats = blogCats
    end function
    
    public function loadTopItemsFromDB()
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")

        sql = " select top 5 a.content_id from blog_content a  where a.blog_id=" & blogId & " order by a.insert_time desc"
        
        call openDB
        
        rs.open sql,conn,1,1

        dim count,i
        count = rs.recordCount
        redim m_topItemIDs(count)
        'response.write count
        if count>0 then
            for i=0 to count -1        
                m_topItemIDs(i) = rs(0)
                rs.moveNext()
            next
        end if    
        rs.close
        set rs = nothing
    end function
    
    public function getTopItems
        if isEmpty(m_topItemIDs) then
            call loadTopItemsFromDB()
            call cache()
        end if    
        
        dim count
        count = ubound(m_topItemIDs)
        if count<1 then
            exit function
        end if 
        
        dim blogItem, i
        redim blogItems(count)
        for i=0 to count-1 
            set blogItem = new BlogItemClass
            call blogItem.load(m_topItemIDs(i))
            set blogItems(i) = blogItem
        next
        
        getTopItems = blogItems
    end function
    
    public function loadTopCommentsFromDB()
        dim i
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")

        sql = " select top 10 a.title,a.is_secret as content_secret, b.blog_id, b.content_id, b.comment_id, b.detail, b.insert_time, b.user_id, b.user_name, b.is_secret, b.email, b.web_url, b.face from blog_content a ,blog_comments b  where a.content_id=b.content_id and b.blog_id=" & blogId & " order by b.insert_time desc"
        
        call openDB
        
        rs.open sql,conn,1,1

        dim count, blogComment
        count = rs.recordCount
        redim m_topComments(count)
        if count>0 then
            for i=0 to count -1    
                set blogComment = new BlogCommentClass
                blogComment.buildFromRS(rs)   
                m_topComments(i) = blogComment.toArray()
                rs.moveNext()
            next
        end if    
        rs.close
        set rs = nothing
    end function
    
        
    public function getTopComments
        if isEmpty(m_topComments) then
            call loadTopCommentsFromDB()
            call cache()
        end if    
        
        dim count
        count = ubound(m_topComments)
        if count<1 then
            exit function
        end if 
        
        dim blogComments, blogComment, i
        redim blogComments(count)
        for i=0 to count-1 
            set blogComment = new blogCommentClass
            call blogComment.build(m_topComments(i))
            set blogComments(i) = blogComment
        next
        
        getTopComments = blogComments
    end function
    

    public function loadLinkGroupsFromDB()
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")

        dim rs2, i
        sql =  " select blog_id, group_id,  group_name from blog_link_group  where blog_id=" & blogId & " order by display_seq"
        
        call openDB
        
        rs.open sql,conn,1,1

        dim count, linkGroup
        count = rs.recordCount
        redim m_linkGroups(count)
        if count>0 then
            for i=0 to count -1    
                set linkGroup = new LinkGroupClass
                linkGroup.buildFromRS(rs)   
                
                dim linkCount, links
                sql = " select blog_id,link_id, blog_id,link_group, link_name, link_url, link_desc, link_img from blog_links where blog_Id=" & blogId & " and link_group=" & linkGroup.groupId & " order by display_seq "
                set rs2 = Server.createObject("ADODB.RecordSet")
                rs2.open sql,conn,1,1
                
                linkCount = rs2.recordCount
                redim links(linkCount)
                if linkCount>0 then
                    dim j, link
                    for j=0 to linkCount-1
                        set link = new LinkClass
                        link.buildFromRS(rs2)
                        set links(j) = link
                        rs2.moveNext()
                    next
                end if
                linkGroup.links = links
                'dim l
                'l = linkGroup.links
                'response.write l(0).linkName
                'response.end
                                
                rs2.close
                set rs2 = nothing
                
                m_linkGroups(i) = linkGroup.toArray()
                rs.moveNext()
            next
        end if    
        rs.close
        set rs = nothing
    end function
    
        
    public function getLinkGroups
        if isEmpty(m_linkGroups) then
            call loadLinkGroupsFromDB()
            call cache()
        end if    
        
        dim count
        count = ubound(m_linkGroups)
        if count<1 then
            exit function
        end if 
        
        dim linkGroups, linkGroup, i
        redim linkGroups(count)
        for i=0 to count-1 
            set linkGroup = new LinkGroupClass
            call linkGroup.build(m_linkGroups(i))
            set linkGroups(i) = linkGroup
        next
        
        getLinkGroups = linkGroups
    end function
    
    
    public function loadNeighborsFromDB()
    
    end function
    
    public function getNeighbors()
    
    end function
    
    
    public function checkUserRight(user_id)
        set checkUserRight = new UserRightClass
        call checkUserRight.load(user_id, blogId, Empty) 
    end function
    
end class


%>